package com.wg.msuser.controller;


import com.wg.msuser.entity.UserAddress;
import com.wg.msuser.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/user-address")
public class UserAddressController {

    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 添加用户地址
     */
    @PostMapping("/add")
    public boolean save(@RequestBody UserAddress userAddress) {
        return userAddressService.save(userAddress);
    }

    /**
     * 删除用户地址
     */
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id) {
        return userAddressService.removeById(id);
    }

    /**
     * 更新用户地址
     */
    @PutMapping("/update")
    public boolean update(@RequestBody UserAddress userAddress) {
        return userAddressService.updateById(userAddress);
    }

    @PutMapping("/set-default/{id}")
    public int setDefault(@PathVariable("id") Integer id) {
        return userAddressService.SetDefault(id);
    }
    /**
     * 将指定用户的所有地址设置为非默认地址
     */
    @PutMapping("/set-all-non-default/{userId}")
    public boolean setAllNonDefault(@PathVariable("userId") Integer userId) {
        return userAddressService.setAllNonDefaultByUserId(userId);
    }
    /**
     * 根据ID获取用户地址
     */
    @GetMapping("/get")
    public UserAddress get(Integer id) {
        return userAddressService.getById(id);
    }

    /**
     * 获取所有用户地址列表
     */
    @GetMapping("/list")
    public List<UserAddress> list() {
        return userAddressService.list();
    }

    /**
     * 根据用户ID获取地址列表
     */
    @GetMapping("/list-by-user/{userId}")
    public List<UserAddress> listByUserId(@PathVariable("userId") Integer userId) {
        return userAddressService.listByUserId(userId);
    }
}

